//The level up system...

function Player1GainExp(amount)
{
  Player.exp = Player.exp + amount;
  Player1Levelup();
}
function Player2GainExp(amount)
{
  Player2.exp = Player2.exp + amount;
  Player2Levelup();
}
function Player3GainExp(amount)
{
  Player3.exp = Player3.exp + amount;
  Player3Levelup();
}

function Player1Levelup()
{
  if (Player.exp >= Player.nextlevel)
  {
    Player.level = Player.level + 1;
    Talk_Window(Player.name + " is now level " + Player.level + "!", Player.port);
    
    LevelUpStatsP1();    
    Player.nextlevel = Math.floor(Player.nextlevel*1.2)
    Player.attack = Player.str + Player.weaponeffect;
    Player.defence = Player.vit + Player.armoureffect;
    Player.mattack = Math.floor(Player.mag*Player.mattackeffect);
    Player.evade = Math.floor(Player.agi/5);
    LearnMagic("Player1");
    LearnTech("Player1");
    LearnSong("Player1");
    LearnRanger("Player1");
    CustomMagic();
    Player1Levelup();
  }
}
function Player2Levelup()
{
  if (Player2.exp >= Player2.nextlevel)
  {
    Player2.level = Player2.level + 1;
    Talk_Window(Player2.name + " is now level " + Player2.level + "!", Player2.port);
    
    LevelUpStatsP2();    
    Player2.nextlevel = Math.floor(Player2.nextlevel*1.2)

    Player2.attack = Player2.str + Player2.weaponeffect;
    Player2.defence = Player2.vit + Player2.armoureffect;
    Player2.mattack = Math.floor(Player2.mag*Player2.mattackeffect);
    Player2.evade = Math.floor(Player2.agi/5);
    LearnMagic("Player2");
    LearnTech("Player2");
    LearnSong("Player2");
    LearnRanger("Player2");
    Player2Levelup();
  }
}
function Player3Levelup()
{
  if (Player3.exp >= Player3.nextlevel)
  {
    Player3.level = Player3.level + 1;
    Talk_Window(Player3.name + " is now level " + Player3.level + "!", Player3.port);
    
    LevelUpStatsP3();    
    Player3.nextlevel = Math.floor(Player3.nextlevel*1.2)

    Player3.attack = Player3.str + Player3.weaponeffect;
    Player3.defence = Player3.vit + Player3.armoureffect;
    Player3.mattack = Math.floor(Player3.mag*Player3.mattackeffect);
    Player3.evade = Math.floor(Player3.agi/5);

    LearnMagic("Player3");
    LearnTech("Player3");
    LearnSong("Player3");
    LearnRanger("Player3");
    Player3Levelup();
  }
}

function LevelUpStatsP1()
{
  if (Player.level <= 10)
  {
    Player.maxhp = Math.floor(Player.maxhp*1.2)
    Player.hp = Math.floor(Player.hp*1.2) 
  }
  else
  {
    if (Player.level <= 30)
    {
      Player.maxhp = Math.floor(Player.maxhp*1.1)
      Player.hp = Math.floor(Player.hp*1.1)
    }
    else
    { 
      Player.maxhp = Math.floor(Player.maxhp*1.025)
      Player.hp = Math.floor(Player.hp*1.025)
    }
  }

  if (Player.chrclass == "Mage")
  {   
    Player.str = Player.str + 1;
    Player.agi = Player.agi + 1;
    Player.vit = Player.vit + 2;
    Player.mag = Player.mag + 3;
    Player.luck = Player.luck + 2;
  }
  else if (Player.chrclass == "Bard")
  {  
    Player.str = Player.str + 2;
    Player.agi = Player.agi + 1;
    Player.vit = Player.vit + 2;
    Player.mag = Player.mag + 2;
    Player.luck = Player.luck + 2;
  }
  else if (Player.chrclass == "Soldier")
  {  
    Player.str = Player.str + 3;
    Player.agi = Player.agi + 2;
    Player.vit = Player.vit + 2;
    Player.mag = Player.mag + 1;
    Player.luck = Player.luck + 1;
  }
  else if (Player.chrclass == "Ranger")
  {
    Player.str = Player.str + 2;
    Player.agi = Player.agi + 3;
    Player.vit = Player.vit + 2;
    Player.mag = Player.mag + 1;
    Player.luck = Player.luck + 1;
  }  
  else
  {
    Player.str = Player.str + 2;
    Player.agi = Player.agi + 2;
    Player.vit = Player.vit + 2;
    Player.mag = Player.mag + 2;
    Player.luck = Player.luck + 1;
  }

Player.maxmp = Player.maxmp + Math.floor(Player.mag/10)
Player.mp = Player.mp + Math.floor(Player.mag/10)

}
function LevelUpStatsP2()
{
  if (Player2.level <= 10)
  {
    Player2.maxhp = Math.floor(Player2.maxhp*1.2)
    Player2.hp = Math.floor(Player2.hp*1.2) 
  }
  else
  {
    if (Player2.level <= 30)
    {
      Player2.maxhp = Math.floor(Player2.maxhp*1.1)
      Player2.hp = Math.floor(Player2.hp*1.1)
    }
    else
    { 
      Player2.maxhp = Math.floor(Player2.maxhp*1.025)
      Player2.hp = Math.floor(Player2.hp*1.025)
    }
  }

  if (Player2.chrclass == "Mage")
  {   
    Player2.str = Player2.str + 1;
    Player2.agi = Player2.agi + 1;
    Player2.vit = Player2.vit + 2;
    Player2.mag = Player2.mag + 3;
    Player2.luck = Player2.luck + 2;
  }
  else if (Player2.chrclass == "Bard")
  {  
    Player2.str = Player2.str + 2;
    Player2.agi = Player2.agi + 1;
    Player2.vit = Player2.vit + 2;
    Player2.mag = Player2.mag + 2;
    Player2.luck = Player2.luck + 2;
  }
  else if (Player2.chrclass == "Soldier")
  {  
    Player2.str = Player2.str + 3;
    Player2.agi = Player2.agi + 2;
    Player2.vit = Player2.vit + 2;
    Player2.mag = Player2.mag + 1;
    Player2.luck = Player2.luck + 1;
  }
  else if (Player2.chrclass == "Ranger")
  {
    Player2.str = Player2.str + 2;
    Player2.agi = Player2.agi + 3;
    Player2.vit = Player2.vit + 2;
    Player2.mag = Player2.mag + 1;
    Player2.luck = Player2.luck + 1;
  }  
  else
  {
    Player2.str = Player2.str + 2;
    Player2.agi = Player2.agi + 2;
    Player2.vit = Player2.vit + 2;
    Player2.mag = Player2.mag + 2;
    Player2.luck = Player2.luck + 1;
  }
  

Player2.maxmp = Player2.maxmp + Math.floor(Player2.mag/10)
Player2.mp = Player2.mp + Math.floor(Player2.mag/10)

}
function LevelUpStatsP3()
{

  if (Player3.level <= 10)
  {
    Player3.maxhp = Math.floor(Player3.maxhp*1.2)
    Player3.hp = Math.floor(Player3.hp*1.2) 
  }
  else
  {
    if (Player3.level <= 30)
    {
      Player3.maxhp = Math.floor(Player3.maxhp*1.1)
      Player3.hp = Math.floor(Player3.hp*1.1)
    }
    else
    { 
      Player3.maxhp = Math.floor(Player3.maxhp*1.025)
      Player3.hp = Math.floor(Player3.hp*1.025)
    }
  }

  if (Player3.chrclass == "Mage")
  {   
    Player3.str = Player3.str + 1;
    Player3.agi = Player3.agi + 1;
    Player3.vit = Player3.vit + 2;
    Player3.mag = Player3.mag + 3;
    Player3.luck = Player3.luck + 2;
  }
  else if (Player3.chrclass == "Bard")
  {  
    Player3.str = Player3.str + 2;
    Player3.agi = Player3.agi + 1;
    Player3.vit = Player3.vit + 2;
    Player3.mag = Player3.mag + 2;
    Player3.luck = Player3.luck + 2;
  }
  else if (Player3.chrclass == "Soldier")
  {  
    Player3.str = Player3.str + 3;
    Player3.agi = Player3.agi + 2;
    Player3.vit = Player3.vit + 2;
    Player3.mag = Player3.mag + 1;
    Player3.luck = Player3.luck + 1;
  }
  else if (Player3.chrclass == "Ranger")
  {
    Player3.str = Player3.str + 2;
    Player3.agi = Player3.agi + 3;
    Player3.vit = Player3.vit + 2;
    Player3.mag = Player3.mag + 1;
    Player3.luck = Player3.luck + 1;
  }  
  else
  {
    Player3.str = Player3.str + 2;
    Player3.agi = Player3.agi + 2;
    Player3.vit = Player3.vit + 2;
    Player3.mag = Player3.mag + 2;
    Player3.luck = Player3.luck + 1;
  }
Player3.maxmp = Player3.maxmp + Math.floor(Player3.mag/10)
Player3.mp = Player3.mp + Math.floor(Player3.mag/10)
}

function LearnMagic(LevelupPerson)
{
  if (LevelupPerson == "Player1")
  {
    if (Player.chrclass == "Mage")
    {
    Player.learnlevel = Player.level;
      if (Player.level == 50)
      {
      Learn("Mega-Cure", "1");
      }
      if (Player.level == 70)
      {
      Learn("Recover", "1");
      }
      if (Player.level == 75)
      {
      Learn("Alpha", "1");
      }
      if (Player.level == 100)
      {
      Learn("Omega", "1");
      }      

    LearnMagic2(1, "1")
    }
    else if (Player.chrclass == "Bard")
    {
    Player.learnlevel = Player.level;  
      if (Player.level == 5)
      {
      Learn("Peep", "1");
      }
    LearnMagic2(1.2, "1")
    }
  }
  else if (LevelupPerson == "Player2")
  {
    if (Player2.chrclass == "Mage")
    {
    Player.learnlevel = Player2.level;

      if (Player2.level == 50)
      {
      Learn("Mega-Cure", "2");
      }
      if (Player2.level == 70)
      {
      Learn("Recover", "2");
      }
      if (Player2.level == 75)
      {
      Learn("Alpha", "2");
      }
      if (Player2.level == 100)
      {
      Learn("Omega", "2");
      }      

    LearnMagic2(1, "2")
    }
    else if (Player2.chrclass == "Bard")
    {
    Player.learnlevel = Player2.level;
      if (Player2.level == 5)
      {
      Learn("Peep", "2");
      }

    LearnMagic2(1.2, "2")
    }
  }
  else if (LevelupPerson == "Player3")
  {
    if (Player3.chrclass == "Mage")
    {
    Player.learnlevel = Player3.level;  
      if (Player3.level == 50)
      {
      Learn("Mega-Cure", "3");
      }
      if (Player3.level == 70)
      {
      Learn("Recover", "3");
      }
      if (Player3.level == 75)
      {
      Learn("Alpha", "3");
      }
      if (Player3.level == 100)
      {
      Learn("Omega", "3");
      }      
    LearnMagic2(1, "3")
    }
    else if (Player3.chrclass == "Bard")
    {
    Player.learnlevel = Player3.level;  
      if (Player3.level == 5)
      {
      Learn("Peep", "3");
      }
    LearnMagic2(1.2, "3")
    }
  }
}
  
function LearnMagic2(a, b)
{  
  if (Player.learnlevel == Math.floor(4*a))
  {Learn("Earth", b);} 

  if (Player.learnlevel == Math.floor(7*a))
  {Learn("Drain Hp", b);} 

  if (Player.learnlevel == Math.floor(10*a))  
  {Learn("Fire 2", b);}

  if (Player.learnlevel == Math.floor(11*a))
  {Learn("Ice 2", b);} 

  if (Player.learnlevel == Math.floor(12*a))
  {Learn("Lit 2", b);} 

  if (Player.learnlevel == Math.floor(15*a))
  {Learn("Earth 2", b);} 

  if (Player.learnlevel == Math.floor(17*a))
  {Learn("Drain Mp", b);} 
    
  if (Player.learnlevel == Math.floor(18*a))
  {Learn("Aero", b);} 

  if (Player.learnlevel == Math.floor(19*a))
  {Learn("Water", b);}
    
  if (Player.learnlevel == Math.floor(25*a))
  {Learn("Fire 3", b);}
    
  if (Player.learnlevel == Math.floor(26*a))
  {Learn("Ice 3", b);}
   
  if (Player.learnlevel == Math.floor(27*a))
  {Learn("Lit 3", b);}
    
  if (Player.learnlevel == Math.floor(30*a))
  {Learn("Earth 3", b);}
    
  if (Player.learnlevel == Math.floor(40*a))
  {Learn("Aera", b);}
    
  if (Player.learnlevel == Math.floor(45*a))
  {Learn("Tidel Wave", b);}
    
  if (Player.learnlevel == Math.floor(60*a))
  {Learn("Metra", b);}

  if (Player.learnlevel == Math.floor(5*a))
  {Learn("Light", b);}
    
  if (Player.learnlevel == Math.floor(8*a))
  {Learn("Curse", b);}
    
  if (Player.learnlevel == Math.floor(12*a))
  {Learn("Life", b);}
    
  if (Player.learnlevel == Math.floor(14*a))
  {Learn("Cure 2", b);}
    
  if (Player.learnlevel == Math.floor(20*a))
  {Learn("Regain", b);}
    
  if (Player.learnlevel == Math.floor(22*a))
  {Learn("Power-Up", b);}
    
  if (Player.learnlevel == Math.floor(23*a))
  {Learn("Defense-Up", b);}
   
  if (Player.learnlevel == Math.floor(24*a))
  {Learn("Haste", b);}
    
  if (Player.learnlevel == Math.floor(35*a))
  {Learn("Cure 3", b);}
    
  if (Player.learnlevel == Math.floor(44*a))
  {Learn("Safe", b);}

  if (Player.learnlevel == Math.floor(47*a))
  {Learn("Life 2", b);}

}

function Learn(magictolearn, Playernumber)
{
  if (Playernumber == "1")
  {
  if (magictolearn == "Earth")
  {Playerbmagic.earth = true;}
  if (magictolearn == "Drain Hp")
  {Playerbmagic.drainhp = true;}
  if (magictolearn == "Fire 2")
  {Playerbmagic.fire2 = true;}
  if (magictolearn == "Ice 2")
  {Playerbmagic.ice2 = true;}
  if (magictolearn == "Lit 2")
  {Playerbmagic.lit2 = true;}
  if (magictolearn == "Earth 2")
  {Playerbmagic.earth2 = true;}
  if (magictolearn == "Drain Mp")
  {Playerbmagic.drainmp = true;}
  if (magictolearn == "Aero")
  {Playerbmagic.aero = true;}
  if (magictolearn == "Water")
  {Playerbmagic.water = true;}
  if (magictolearn == "Fire 3")
  {Playerbmagic.fire3 = true;}
  if (magictolearn == "Ice 3")
  {Playerbmagic.ice3 = true;}
  if (magictolearn == "Lit 3")
  {Playerbmagic.lit3 = true;}
  if (magictolearn == "Earth 3")
  {Playerbmagic.earth3 = true;}
  if (magictolearn == "Aera")
  {Playerbmagic.aera = true;}
  if (magictolearn == "Tidel Wave")
  {Playerbmagic.tidelwave = true;}
  if (magictolearn == "Metra")
  {Playerbmagic.metra = true;}
  if (magictolearn == "Alpha")
  {Playerbmagic.alpha = true;}
  if (magictolearn == "Omega")
  {Playerbmagic.omega = true;}
  
  if (magictolearn == "Peep")
  {Playerwmagic.peep = true;}  
  if (magictolearn == "Light")
  {Playerwmagic.light = true;}
  if (magictolearn == "Curse")
  {Playerwmagic.curse = true;}
  if (magictolearn == "Life")
  {Playerwmagic.life1 = true;}
  if (magictolearn == "Cure 2")
  {Playerwmagic.cure2 = true;}
  if (magictolearn == "Regain")
  {Playerwmagic.regain = true;}
  if (magictolearn == "Power-Up")
  {Playerwmagic.powerup = true;}
  if (magictolearn == "Defense-Up")
  {Playerwmagic.defup = true;}
  if (magictolearn == "Haste")
  {Playerwmagic.haste = true;}
  if (magictolearn == "Cure 3")
  {Playerwmagic.cure3 = true;}
  if (magictolearn == "Safe")
  {Playerwmagic.safe = true;}
  if (magictolearn == "Life 2")
  {Playerwmagic.life2 = true;}
  if (magictolearn == "Mega-Cure")
  {Playerwmagic.megacure = true;}
  if (magictolearn == "Recover")
  {Playerwmagic.recover = true;}
  }

  if (Playernumber == "2")
  {
  if (magictolearn == "Lit")
  {Player2bmagic.lit = true;}  
  if (magictolearn == "Earth")
  {Player2bmagic.earth = true;}
  if (magictolearn == "Drain Hp")
  {Player2bmagic.drainhp = true;}
  if (magictolearn == "Fire 2")
  {Player2bmagic.fire2 = true;}
  if (magictolearn == "Ice 2")
  {Player2bmagic.ice2 = true;}
  if (magictolearn == "Lit 2")
  {Player2bmagic.lit2 = true;}
  if (magictolearn == "Earth 2")
  {Player2bmagic.earth2 = true;}
  if (magictolearn == "Drain Mp")
  {Player2bmagic.drainmp = true;}
  if (magictolearn == "Aero")
  {Player2bmagic.aero = true;}
  if (magictolearn == "Water")
  {Player2bmagic.water = true;}
  if (magictolearn == "Fire 3")
  {Player2bmagic.fire3 = true;}
  if (magictolearn == "Ice 3")
  {Player2bmagic.ice3 = true;}
  if (magictolearn == "Lit 3")
  {Player2bmagic.lit3 = true;}
  if (magictolearn == "Earth 3")
  {Player2bmagic.earth3 = true;}
  if (magictolearn == "Aera")
  {Player2bmagic.aera = true;}
  if (magictolearn == "Tidel Wave")
  {Player2bmagic.tidelwave = true;}
  if (magictolearn == "Metra")
  {Player2bmagic.metra = true;}
  if (magictolearn == "Alpha")
  {Player2bmagic.alpha = true;}
  if (magictolearn == "Omega")
  {Player2bmagic.omega = true;}
  
  if (magictolearn == "Peep")
  {Player2wmagic.peep = true;}  
  if (magictolearn == "Light")
  {Player2wmagic.light = true;}
  if (magictolearn == "Curse")
  {Player2wmagic.curse = true;}
  if (magictolearn == "Life")
  {Player2wmagic.life1 = true;}
  if (magictolearn == "Cure 2")
  {Player2wmagic.cure2 = true;}
  if (magictolearn == "Regain")
  {Player2wmagic.regain = true;}
  if (magictolearn == "Power-Up")
  {Player2wmagic.powerup = true;}
  if (magictolearn == "Defense-Up")
  {Player2wmagic.defup = true;}
  if (magictolearn == "Haste")
  {Player2wmagic.haste = true;}
  if (magictolearn == "Cure 3")
  {Player2wmagic.cure3 = true;}
  if (magictolearn == "Safe")
  {Player2wmagic.safe = true;}
  if (magictolearn == "Life 2")
  {Player2wmagic.life2 = true;}
  if (magictolearn == "Mega-Cure")
  {Player2wmagic.megacure = true;}
  if (magictolearn == "Recover")
  {Player2wmagic.recover = true;}
  }
  
  if (Playernumber == "3")
  {
  if (magictolearn == "Lit")
  {Player3bmagic.lit = true;}  
  if (magictolearn == "Earth")
  {Player3bmagic.earth = true;}
  if (magictolearn == "Drain Hp")
  {Player3bmagic.drainhp = true;}
  if (magictolearn == "Fire 2")
  {Player3bmagic.fire2 = true;}
  if (magictolearn == "Ice 2")
  {Player3bmagic.ice2 = true;}
  if (magictolearn == "Lit 2")
  {Player3bmagic.lit2 = true;}
  if (magictolearn == "Earth 2")
  {Player3bmagic.earth2 = true;}
  if (magictolearn == "Drain Mp")
  {Player3bmagic.drainmp = true;}
  if (magictolearn == "Aero")
  {Player3bmagic.aero = true;}
  if (magictolearn == "Water")
  {Player3bmagic.water = true;}
  if (magictolearn == "Fire 3")
  {Player3bmagic.fire3 = true;}
  if (magictolearn == "Ice 3")
  {Player3bmagic.ice3 = true;}
  if (magictolearn == "Lit 3")
  {Player3bmagic.lit3 = true;}
  if (magictolearn == "Earth 3")
  {Player3bmagic.earth3 = true;}
  if (magictolearn == "Aera")
  {Player3bmagic.aera = true;}
  if (magictolearn == "Tidel Wave")
  {Player3bmagic.tidelwave = true;}
  if (magictolearn == "Metra")
  {Player3bmagic.metra = true;}
  if (magictolearn == "Alpha")
  {Player3bmagic.alpha = true;}
  if (magictolearn == "Omega")
  {Player3bmagic.omega = true;}
  
  if (magictolearn == "Peep")
  {Player3wmagic.peep = true;}  
  if (magictolearn == "Light")
  {Player3wmagic.light = true;}
  if (magictolearn == "Curse")
  {Player3wmagic.curse = true;}
  if (magictolearn == "Life")
  {Player3wmagic.life1 = true;}
  if (magictolearn == "Cure 2")
  {Player3wmagic.cure2 = true;}
  if (magictolearn == "Regain")
  {Player3wmagic.regain = true;}
  if (magictolearn == "Power-Up")
  {Player3wmagic.powerup = true;}
  if (magictolearn == "Defense-Up")
  {Player3wmagic.defup = true;}
  if (magictolearn == "Haste")
  {Player3wmagic.haste = true;}
  if (magictolearn == "Cure 3")
  {Player3wmagic.cure3 = true;}
  if (magictolearn == "Safe")
  {Player3wmagic.safe = true;}
  if (magictolearn == "Life 2")
  {Player3wmagic.life2 = true;}
  if (magictolearn == "Mega-Cure")
  {Player3wmagic.megacure = true;}
  if (magictolearn == "Recover")
  {Player3wmagic.recover = true;}
  }
  if (Playernumber == "1")
  {
  Talk_Window(Player.name + " learned " + magictolearn + "!", Player.port);
  }
  if (Playernumber == "2")
  {
  Talk_Window(Player2.name + " learned " + magictolearn + "!", Player2.port);
  }
  if (Playernumber == "3")
  {
  Talk_Window(Player3.name + " learned " + magictolearn + "!", Player3.port);
  }
}

function LearnTech(TechupPerson)
{
  if (TechupPerson == "Player1")
  {
    if (Player.chrclass == "Soldier")
    {
      if (Player.level == 14)
      {
      Playersoldier.dualcombo = true;
      Talk_Window(Player.name + " Learned Dual Combo!", Player.port);
      }
      if (Player.level == 22)
      {
      Playersoldier.sworddrain = true;
      Talk_Window(Player.name + " Learned Sword Drain!", Player.port);
      }
      if (Player.level == 35)
      {
      Playersoldier.charge = true;
      Talk_Window(Player.name + " Learned Charge!", Player.port);
      }
      if (Player.level == 50)
      {
      Playersoldier.quadslice = true;
      Talk_Window(Player.name + " Learned Quad Slice!", Player.port);
      }      
    }
  }
  else if (TechupPerson == "Player2")
  {
    if (Player2.chrclass == "Soldier")
    {
      if (Player2.level == 14)
      {
      Player2soldier.dualcombo = true;
      Talk_Window(Player2.name + " Learned Dual Combo!", Player2.port);
      }
      if (Player2.level == 22)
      {
      Player2soldier.sworddrain = true;
      Talk_Window(Player2.name + " Learned Sword Drain!", Player2.port);
      }
      if (Player2.level == 35)
      {
      Player2soldier.charge = true;
      Talk_Window(Player2.name + " Learned Charge!", Player2.port);
      }
      if (Player2.level == 50)
      {
      Player2soldier.quadslice = true;
      Talk_Window(Player2.name + " Learned Quad Slice!", Player2.port);
      }      
    }
  }
  else if (TechupPerson == "Player3")
  {
    if (Player3.chrclass == "Soldier")
    {
      if (Player3.level == 14)
      {
      Player3soldier.dualcombo = true;
      Talk_Window(Player3.name + " Learned Dual Combo!", Player3.port);
      }
      if (Player3.level == 22)
      {
      Player3soldier.sworddrain = true;
      Talk_Window(Player3.name + " Learned Sword Drain!", Player3.port);
      }
      if (Player3.level == 35)
      {
      Player3soldier.charge = true;
      Talk_Window(Player3.name + " Learned Charge!", Player3.port);
      }
      if (Player3.level == 50)
      {
      Player3soldier.quadslice = true;
      Talk_Window(Player3.name + " Learned Quad Slice!", Player3.port);
      }      
    }
  }
}

function LearnSong(Bardperson)
{
  if (Bardperson == "Player1")
  {
    if (Player.chrclass == "Bard")
    {
      if (Player.level == 6)
      {
      Playerbard.intimidate = true;
      Talk_Window(Player.name + " Learned Intimidate!", Player.port);
      }
      if (Player.level == 18)
      {
      Playerbard.animals = true;
      Talk_Window(Player.name + " Learned Animals!", Player.port);
      }
      if (Player.level == 32)
      {
      Playerbard.resonance = true;
      Talk_Window(Player.name + " Learned Resonance!", Player.port);
      }
    }
  }
  else if (Bardperson == "Player2")
  {
    if (Player2.chrclass == "Bard")
    {
      if (Player2.level == 6)
      {
      Player2bard.intimidate = true;
      Talk_Window(Player2.name + " Learned Intimidate!", Player2.port);
      }
      if (Player2.level == 18)
      {
      Player2bard.animals = true;
      Talk_Window(Player2.name + " Learned Animals!", Player2.port);
      }
      if (Player2.level == 32)
      {
      Player2bard.resonance = true;
      Talk_Window(Player2.name + " Learned Resonance!", Player2.port);
      }
    }
  }
  else if (Bardperson == "Player3")
  {
    if (Player3.chrclass == "Bard")
    {
      if (Player3.level == 6)
      {
      Player3bard.intimidate = true;
      Talk_Window(Player3.name + " Learned Intimidate!", Player3.port);
      }
      if (Player3.level == 18)
      {
      Player3bard.animals = true;
      Talk_Window(Player3.name + " Learned Animals!", Player3.port);
      }
      if (Player3.level == 32)
      {
      Player3bard.resonance = true;
      Talk_Window(Player3.name + " Learned Resonance!", Player3.port);
      }
    }
  }
}

function CustomMagic()
{
  if (Player.customnumber < 11)
  {
    if (Player.chrclass == "Mage")
    {
    Player.customlearn = Player.customlearn - 1;
      if (Player.customlearn >= 0)
      {
      }
      else
      {
        LearnCustom(Player.customlearning) 
      }
    }
  }
  else 
  {
  }
}

function LearnCustom(customtolearn)
{
  if (customtolearn == "Nothing")
  {
  SelectNewCustom();
  }
  else if (customtolearn == "Telekinesis")
  {
  PromptForName("Uses the mind to cause physical damage on a target.  Default name: Telekinesis");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.telekinesispres = true;
  Player.cmagic.telekinesis = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Diease")
  {
  PromptForName("Creates a illness that damages a target.  Default name: Diease");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.dieasepres = true;
  Player.cmagic.diease = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Explode")
  {
  PromptForName("Creates an explosion that deals fire damage to all targets.  Default name: Explode");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.explodepres = true;
  Player.cmagic.explode = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Explode 2")
  {
  PromptForName("Creates a very powerful explosion that engulfs all targets.  Default name: Explode 2");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.explode2pres = true;
  Player.cmagic.explode2 = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Plague")
  {
  PromptForName("Creates a deadly illness that damages all targets.  Default name: Plague");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.plaguepres = true;
  Player.cmagic.plague = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Flare")
  {
  PromptForName("A very powerful magic that causes non-elemental damage to one target.  Default name: Flare");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.flarepres = true;
  Player.cmagic.flare = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Hold")
  {
  PromptForName("A simple magic that prevents a monster from moving during battle.  Default name: Hold");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.holdpres = true;
  Player.cmagic.hold = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Encounter")
  {
  PromptForName("A magic that can changes the rate in which the party will enage in a battle.  Default name: Encounter");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.encounterpres = true;
  Player.cmagic.encounter = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Multi-Up")
  {
  PromptForName("A magic that increases the attack, defense, and evade of one person.  Default name: Multi-Up");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.multiuppres = true;
  Player.cmagic.multiup = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Mp Regain")
  {
  PromptForName("A magic that recovers a person's mp over time.  Default name: Mp Regain");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.mpregainpres = true;
  Player.cmagic.mpregain = Player.customtemp;
  SelectNewCustom();
  }
  else if (customtolearn == "Holy Beam")
  {
  PromptForName("A magic that casts a beam of holy light on a target.  Default name: Holy Beam");
  Player.customnumber = Player.customnumber + 1;
  Player.cmagic.anglicraypres = true;
  Player.cmagic.anglicray = Player.customtemp;
  SelectNewCustom();
  }
}

function PromptForName(customdescription)
{
  RenderMap();
  eatKey();
  WindowStyle.drawWindow (5, 5, 630, 60)
  Font.drawText(8, 8, "You have created a new magic!");
  Font.drawTextBox(8, 38, 625, 60, 5, customdescription);
  
  var prompt = "Enter a name: ";
  Font.drawText(8, 18, prompt);
  Player.customtemp = GetString(8 + Font.getStringWidth(prompt), 18, GradientFont);

  if (Player.customtemp == "")
  {
  WindowStyle.drawWindow (25, 380, 590, 80);
  Font.drawTextBox (75, 400, 525, 60, 5, "You must select a name!");//75, 380, 525, 100, 5
  FlipScreen();
  Delay(1000);
  eatKey();
  PromptForName(customdescription);
  }
}

function SelectNewCustom()
{
  RenderMap();
  
  WindowStyle.drawWindow(8, 8, 472, 12);
  Font.drawText(10, 10, "Select magic to create:");

  var ItemMenu = new Menu();
  ItemMenu.font         = Font;
  ItemMenu.window_style = WindowStyle;
  ItemMenu.arrow        = Arrow;

  if (Player.cmagic.dieasepres == false)
  {
  ItemMenu.addItem("Magic attack (Sickness), Mp cost: 12", CustomMagicLearnDiease);
  }

  if (Player.cmagic.holdpres == false)
  {
  ItemMenu.addItem("Monster restriction magic, Mp cost: 15", CustomMagicLearnHold);
  }

  if (Player.level >=  10)
  {
    if (Player.cmagic.telekinesispres == false)
    {
    ItemMenu.addItem("Physical magic attack, Mp cost: 15", CustomMagicLearnTelekinesis);
    }
    if (Player.cmagic.encounterpres == false)
    {
    ItemMenu.addItem("Misc magic, Mp cost: 15", CustomMagicLearnEncounter);
    }
  }
  if (Player.level >=  20)
  {
    if (Player.cmagic.explodepres == false)
    {
    ItemMenu.addItem("Magic attack (Fire), Mp cost: 30", CustomMagicLearnExplode);
    }
    if (Player.cmagic.multiuppres == false)
    {
    ItemMenu.addItem("Status increase, Mp cost: 45", CustomMagicLearnMultiup);
    }
  }
  if (Player.level >=  30)
  {
    if (Player.cmagic.explode2pres == false)
    {
      if (Player.cmagic.explodepres == true)
      {
      ItemMenu.addItem("Magic attack (Fire), Mp cost: 50", CustomMagicLearnExplode2);
      }
    }
    if (Player.cmagic.mpregainpres == false)
    {
    ItemMenu.addItem("Mp restore, Mp cost: 75", CustomMagicLearnMpregain);
    }
  }
  if (Player.level >=  40)
  {
    if (Player.cmagic.plaguepres == false)
    {
    ItemMenu.addItem("Magic attack (Sickness), Mp cost: 60", CustomMagicLearnPlague);
    }
  }
  if (Player.level >=  50)
  {
    if (Player.cmagic.flarepres == false)
    {
    ItemMenu.addItem("Magic attack (Non-elemental), Mp cost: 75", CustomMagicLearnFlare);
    }
    if (Player.cmagic.anglicraypres == false)
    {
    ItemMenu.addItem("Magic attack (Holy), Mp cost: 75", CustomMagicLearnAnglicray);
    }
  }
  ItemMenu.addItem("Nothing (Wait for next level)", CustomMagicLearnNothing);
  ItemMenu.execute(8, 34, 472, 140);
}

function CustomMagicLearnNothing()
{
Player.customlearn = 0;
Player.customlearning = "Nothing";
}
function CustomMagicLearnDiease()
{
Player.customlearn = 6;
Player.customlearning = "Diease";
}
function CustomMagicLearnTelekinesis()
{
Player.customlearn = 7;
Player.customlearning = "Telekinesis";
}
function CustomMagicLearnExplode()
{
Player.customlearn = 8;
Player.customlearning = "Explode";
}
function CustomMagicLearnExplode2()
{
Player.customlearn = 9;
Player.customlearning = "Explode 2";
}
function CustomMagicLearnPlague()
{
Player.customlearn = 9;
Player.customlearning = "Plague";
}
function CustomMagicLearnFlare()
{
Player.customlearn = 10;
Player.customlearning = "Flare";
}
function CustomMagicLearnHold()
{
Player.customlearn = 6;
Player.customlearning = "Hold";
}
function CustomMagicLearnEncounter()
{
Player.customlearn = 7;
Player.customlearning = "Encounter";
}
function CustomMagicLearnMultiup()
{
Player.customlearn = 8;
Player.customlearning = "Multi-Up";
}
function CustomMagicLearnMpregain()
{
Player.customlearn = 9;
Player.customlearning = "Mp Regain";
}
function CustomMagicLearnAnglicray()
{
Player.customlearn = 10;
Player.customlearning = "Holy Beam";
}


